#include <stdlib.h>

#include <windows.h>

#include "lib.h"
#include "casbase.h"

#define INIFILE	TEXT("server.ini")
#define USRFILE	TEXT("users.ini") // CSN-8334 - Move USERS stats out of server.ini
#define USRSTAT	TEXT("USERS")

String CasinoBase::basename;
int32 CasinoBase::baseport = 0;

void 
CasinoBase::load()
{
	uint16 inifile[MAX_PATH + 1];
	int32 len;

	len = GetModuleFileNameW(NULL, inifile, MAX_PATH);
	while (len && inifile[len - 1] != '\\')
		len--;
	wcscpy(inifile + len, INIFILE);

	getStrOpt("Base", "name", "", basename, inifile);
	baseport = getInt32Opt("Base", "port", 0, inifile);
}

void
CasinoBase::getStrOpt(const String& Section, const String& Key, const String& Default,
	String& Dest, const String& IniFile)
{
	int32 len = 2048;

	len = GetPrivateProfileString(Section, Key, Default,
		Dest.getBuffer(len), len, IniFile);

	Dest.releaseBuffer();

	Dest.trim();
}

void
CasinoBase::getStrOptEx(const String& Section, const String& Key, const String& Default,
	String& Dest)
{
	

    uint16 inifile[MAX_PATH + 1];
	int32 len;

	len = GetModuleFileNameW(NULL, inifile, MAX_PATH);
	while (len && inifile[len - 1] != '\\')
		len--;
	
	// CSN-8334 - Move USERS stats out of server.ini
	wcscpy(inifile + len, (wcscmp(Key, USRSTAT)) ? INIFILE : USRFILE);

    len = 2048;
	len = GetPrivateProfileString(Section, Key, Default,
		Dest.getBuffer(len), len, inifile);

	Dest.releaseBuffer();

	Dest.trim();
}

int
CasinoBase::setStrOpt(const String& Section, const String& Key, 
	String& Dest, const String& IniFile)
{
	int bRet;
    Dest.trim();
 
	bRet = WritePrivateProfileString(Section, Key,Dest, IniFile);

	return bRet;
}
int
CasinoBase::setStrOptEx(const String& Section, const String& Key, 
	const String& Dest)
{
	int bRet;

    uint16 inifile[MAX_PATH + 1];
	int32 len;

	len = GetModuleFileNameW(NULL, inifile, MAX_PATH);
	while (len && inifile[len - 1] != '\\')
		len--;
	
	// CSN-8334 - Move USERS stats out of server.ini
	wcscpy(inifile + len, (wcscmp(Key, USRSTAT)) ? INIFILE : USRFILE);
   
    bRet = WritePrivateProfileString(Section, Key,Dest, inifile);
	return bRet;
}

int32
CasinoBase::getInt32Opt(const String& Section, const String& Key, int32 Default, const String& IniFile)
{
	return (int32)GetPrivateProfileInt(Section, Key, (INT)Default, IniFile);
}
